﻿Namespace IO
    Public Class BlockStreamWriter
        Private mStream As System.IO.Stream
        Public Sub New(ByVal stream As System.IO.Stream)
            mStream = stream
        End Sub

        Public Sub WriteFixedLengthAscii(ByVal text As String)
            Dim bText() As Byte = System.Text.Encoding.ASCII.GetBytes(text)
            For Each c As Byte In bText
                Write(c)
            Next
        End Sub

        Public Sub Write(ByVal text As String)
            Dim bText() As Byte = System.Text.Encoding.UTF8.GetBytes(text)
            Write(bText)
        End Sub
        Public Sub Write(ByVal value As Integer)
            For i As Integer = 0 To 3
                Dim iVal As Integer = value Mod 256
                Write(CByte(iVal))
                value = value \ 256
            Next
        End Sub
        Public Sub Write(ByVal data() As Byte)
            Write(mStream, data)
        End Sub
        Public Sub Write(ByVal value As Boolean)
            Write(mStream, value)
        End Sub
        Public Sub Write(ByVal value As Byte)
            mStream.WriteByte(value)
        End Sub
        'Public Sub Write(ByVal value As Short)
        '    For i As Integer = 0 To 1
        '        Dim iVal As Short = CShort(value Mod 256)
        '        Write(CByte(iVal))
        '        value = CShort(value \ 256)
        '    Next
        'End Sub

        Public Shared Sub Write(ByVal stream As System.IO.Stream, ByVal data() As Byte)
            If data Is Nothing Then
                Write(stream, 0)
            ElseIf data.Length = 0 Then
                Write(stream, 0)
            Else
                Write(stream, data.Length)
                stream.Write(data, 0, data.Length)
            End If
        End Sub
        Public Shared Sub Write(ByVal stream As System.IO.Stream, ByVal data() As Byte, ByVal offset As Integer, ByVal count As Integer)
            If data Is Nothing Then
                Write(stream, 0)
            ElseIf data.Length = 0 Then
                Write(stream, 0)
            Else
                Write(stream, count)
                stream.Write(data, offset, count)
            End If
        End Sub

        Public Shared Sub BeginWrite(ByVal stream As System.IO.Stream, ByVal data() As Byte)
            If data Is Nothing Then
                Write(stream, 0)
            ElseIf data.Length = 0 Then
                Write(stream, 0)
            Else
                Write(stream, data.Length)
                stream.BeginWrite(data, 0, data.Length, Nothing, Nothing)
            End If
        End Sub
        Public Shared Sub BeginWrite(ByVal stream As System.IO.Stream, ByVal data() As Byte, ByVal offset As Integer, ByVal count As Integer)
            If data Is Nothing Then
                Write(stream, 0)
            ElseIf data.Length = 0 Then
                Write(stream, 0)
            Else
                Write(stream, count)
                stream.BeginWrite(data, offset, count, Nothing, Nothing)
            End If
        End Sub

        Public Shared Sub Write(ByVal stream As System.IO.Stream, ByVal value As Integer)
            stream.Write(BitConverter.GetBytes(value), 0, 4)
        End Sub
        Public Shared Sub Write(ByVal stream As System.IO.Stream, ByVal value As Long)
            stream.Write(BitConverter.GetBytes(value), 0, 8)
        End Sub
        Public Shared Sub Write(ByVal stream As System.IO.Stream, ByVal value As Short)
            stream.Write(BitConverter.GetBytes(value), 0, 2)
        End Sub
        Public Shared Sub Write(ByVal stream As System.IO.Stream, ByVal value As Byte)
            stream.WriteByte(value)
        End Sub
        Public Shared Sub Write(ByVal stream As System.IO.Stream, ByVal value As Boolean)
            stream.WriteByte(CByte(IIf(value, 1, 0)))
        End Sub
        Public Shared Sub Write(ByVal stream As System.IO.Stream, ByVal text As String)
            If text Is Nothing Then text = String.Empty
            Dim bText() As Byte = System.Text.Encoding.UTF8.GetBytes(text)
            Write(stream, bText)
        End Sub
        Public Shared Sub WriteFixedLengthAscii(ByVal stream As System.IO.Stream, ByVal text As String)
            Dim bText() As Byte = System.Text.Encoding.ASCII.GetBytes(text)
            For Each c As Byte In bText
                Write(stream, c)
            Next
        End Sub


    End Class

End Namespace